import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:yeyu_net/request/hi_base_request.dart';
import 'hi_error.dart';
import 'hi_net_adapter.dart';

class HttpAdapter extends HiNetAdapter {
  @override
  // ignore: missing_return
  Future<HiNetResponse<T>> send<T>(HiBaseRequest request) async {
    var response;
    var client = http.Client();
    var header = request.header;
    var error;
    try {
      if (response.httpMethod() == HttpMethod.GET) {
        response = await client.get(Uri.parse(request.url()), headers: header);
      } else if (response.httpMethod() == HttpMethod.POST) {
        response = await client.post(Uri.parse(request.url()),
            headers: header, body: request.params);
      } else if (response.httpMethod() == HttpMethod.DELETE) {
        response = await client.delete(Uri.parse(request.url()),
            headers: header, body: request.params);
      }
    } on HttpException catch (e) {
      error = e;
    }
    if (error != null) {
      //  抛出HiNetError
      throw HiNetError(response?.statusCode ?? -1, error.toString());
    }
  }
}
